home *** CD-ROM | disk | FTP | other *** search
- Path: news.nask.org.pl!usenet
- From: flssoft@blue.maloka.waw.pl (Grzegorz (FLS))
- Newsgroups: comp.lang.c++
- Subject: Re: Unions
- Date: Tue, 02 Apr 1996 23:44:07 GMT
- Organization: Research and Academic Computer Network
- Message-ID: <4jsads$1a3@bilbo.nask.org.pl>
- References: <rossfeld.1.00111D6B@ucla.edu>
- NNTP-Posting-Host: s110.maloka.waw.pl
- X-Newsreader: Forte Free Agent v0.46
-
- rossfeld@ucla.edu (James E Rossfeld) wrote:
-
- >Could someone give me a detailed explanation of what the advantages are to
- >using unions (if any). I'm still a little unfamiliar with how they work.
-
- Hi,
-
- Imagine that you write a calc parser that could have saved variables.
- You can use it in such way:
-
- First=5
- TheSecond=10
- ?=First
- First=First+TheSecond*10
-
- etc
-
- When you analyse each line, for example 'First=First+TheSecond*10',
- you collect (build) special tree like: (sorry, there is only a text)
-
- =
- / \
- First +
- / \
- First *
- / \
- TheSecond 10
-
- This is a prefix tree. You analyse the left side first, next you
- analyse the operator, and next analyse the right side. For example,
- first you get 'First' variable, next you get '=' sign, and you know,
- that you have to save in 'First' variable the result of right subtree.
- But lets return to our union.
- Each node of that tree could save:
- operators like '=', '-', '+'
- variables like 'First', 'TheSecond'
- values like 10, 3.1415926
-
- Lets assume that we do not use unions. In this way we write a
- structure for each node like:
-
- struct SNode
- {
- // What we are saving here in node.
- enum
- {
- SN_OPERATOR
- , SN_VARIABLE
- , SN_VALUE
-
- } m_what_is_here ;
-
- // The content of node.
- char m_operator ; // like '=', '-', '*', '/' etc
- char* m_p_variable_name ;
- double m_value ;
-
- // Left and right subtrees.
- SNode* m_pLeftNode ;
- SNode* m_pRightNode ;
- } ;
-
-
- Lets count the sizeof( SNode ) for FAR memory model (on PC)
-
- m_what_is_here = sizeof( int ) = 2
- m_operator = sizeof( char ) = 2
- m_p_variable_name = sizeof( char* ) = 4
- m_value = sizeof( double ) = 8
- m_pLeftNode = sizeof( SNode* ) = 4
- m_pLeftRight = sizeof( SNode* ) = 4
- -------------------------------------------
- sizeof( SNode ) = 24
-
-
- Note that in _every_ node you reserve memory for each situation
- (variable, operator, value), however we are sure, that there is only
- one possible state of each node. There is no possibility to have in
- the same node both variable and value (for example). Then for what we
- declare this memory? It is only a waste of it.
- Lets optimize it. Now we will use an union.
-
- struct SNode
- {
- // What we are saving here in node.
- enum
- {
- SN_OPERATOR
- , SN_VARIABLE
- , SN_VALUE
-
- } m_what_is_here ;
-
- // The content of node.
- union
- {
- char m_operator ; // like '=', '-', '*', '/' etc
- char* m_p_variable_name ;
- double m_value ;
- }
-
- // Left and right subtrees.
- SNode* m_pLeftNode ;
- SNode* m_pRightNode ;
- } ;
-
-
- What happened? What gives us this union?
- Lets count the sum of sizes for each members of union.
-
- m_operator = sizeof( char ) = 2
- m_p_variable_name = sizeof( char* ) = 4
- m_value = sizeof( double ) = 8
- -------------------------------------------
- = 14
-
- and what is the size of the union? Lets look:
-
- The size is max( 2, 4, 8 ) = 8
-
- The compile declare the space for the biggest member of the union. Now
- each node will have more less size - 18 (not 24). You save 25% of
- memory. In this way you can build the bigger tree, You can add 25%
- more nodes.
-
- A union saves you memory, gives you a different look at declaration.
- There is no need to use it often. Only sometimes (like in the example)
- there is need to do it.
-
- I hope it will explain you a union.
- Maybe my english won't disturb you? ;)
-
- Good luck,
- Grzegorz.
-
-
-